package com.googlecode.contests_repo.servlets;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

import com.googlecode.contests_repo.core.WebTools;
import com.googlecode.contests_repo.core.model.ResourcesUtils;
import com.googlecode.contests_repo.core.model.Series;
import com.googlecode.contests_repo.validation.ValidationTools;

/**
 * Servlet implementation class SeriesEditServlet
 */
@WebServlet("/SeriesEditServlet")
public class SeriesEditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String relativeSeriesPath = request.getParameter("path");
		Series oldSeries = getOldFieldData(request);
		Series newSeries = getNewFieldData(request);
		Series currentSeries = getCurrentResource(relativeSeriesPath);

		if (!oldSeries.getTitle().equals(newSeries.getTitle())) {
			currentSeries.setTitle(newSeries.getTitle());
		}
		if (!oldSeries.getAbout().equals(newSeries.getAbout())) {
			currentSeries.setAbout(newSeries.getAbout());
		}
		if (!oldSeries.getNotes().equals(newSeries.getNotes())) {
			currentSeries.setNotes(newSeries.getNotes());
		}

		// validate
		String fullSeriesPath = ResourcesUtils.getFullResourcePath(relativeSeriesPath);
		List<String> errors = ValidationTools.validate(relativeSeriesPath, Series.class);
		
		// save series.json file
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.writeValue(new File(fullSeriesPath, "series.json"),
					currentSeries);
		} catch (Exception e) {
			errors.add(e.getMessage());
		}

		// send response
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(WebTools.buildErrorString(errors, true));
	}

	private Series getOldFieldData(HttpServletRequest request) {
		Series series = new Series();
		series.setId(request.getParameter("id"));
		series.setAbout(request.getParameter("old-about"));
		series.setNotes(request.getParameter("old-notes"));
		series.setTitle(request.getParameter("old-title"));

		return series;
	}

	private Series getNewFieldData(HttpServletRequest request) {
		Series series = new Series();
		series.setId(request.getParameter("id"));
		series.setAbout(request.getParameter("about"));
		series.setNotes(request.getParameter("notes"));
		series.setTitle(request.getParameter("title"));

		return series;
	}

	private Series getCurrentResource(String relativeResourcePath) {
		String resourcePath = ResourcesUtils.getFullResourcePath(relativeResourcePath);

		return ResourcesUtils.getJSON(new File(resourcePath));
	}
}
